<!--
 * @Author: your name
 * @Date: 2021-03-12 10:58:48
 * @LastEditTime: 2021-03-12 14:49:52
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ui-demo\src\components\AddUser.vue
-->
<template>
  <div>
    <el-form ref="editUser" :model="editUser" :rules="rules" label-width="100px">
      <el-form-item label="账号" prop="username">
        <el-input v-model="editUser.username" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="editUser.password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('editUser')">修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { editUser } from '@/api/user.js'
import { Message } from 'element-ui'

export default {
  name: 'EditUser',
  props: {
    editUser: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          editUser(this.editUser.userId, this.editUser).then(response => {
            if (response.code === 0) {
              Message({
                message: '修改用户成功',
                type: 'success',
                duration: 3 * 1000
              })
              this.$emit('submit')
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
